package fr.pentalog.ctc.controller.page;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ro.peoplecentric.ctc.common.entity.Item;
import ro.peoplecentric.ctc.common.exception.ApplicationException;
import ro.peoplecentric.ctc.common.service.ItemService;
import fr.pentalog.ctc.util.WebAppConstants;


/**
 * @author Danut Chindris
 * @since September 12, 2013
 */

@Controller
public class HomeController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	public static final String HOME_VIEW_NAME = "pages/home";
	public static final String HOME_PAGE = "home";

	@Resource
	private ItemService itemService;

	@RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
	public String showHomePage(Model model) {
		List<Item> items = null;
		try {
			items = itemService.getLastModifiedItems();
		}
		catch (ApplicationException e) {
			logger.error(e.getMessage(), e);
		}
		if (items != null && items.size() != WebAppConstants.ZERO) {
			model.addAttribute(WebAppConstants.ARTICLES_ATTRIBUTE, items);
		}
		model.addAttribute(WebAppConstants.ACTIVE_PAGE, HOME_PAGE);
		return HOME_VIEW_NAME;
	}
}
